// JavaScript Document
//页面加载方法
function loadImages() {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById('hidepage').style.visibility = 'hidden';
    } else {
        if (document.layers) { // Netscape 4
            document.hidepage.visibility = 'hidden';
        } else { // IE 4
            document.all.hidepage.style.visibility = 'hidden';
        }
    }
    /*  var openid=$.session.get("platformId");
    if(typeof openid == "undefined"||openid==""){
    	location.href = "../returnMsg.jsp?isapp=0"
		//layer.open({content: '请先注册！',time: 2,end: function(){location.href = "../returnMsg.jsp?isapp=0"}});
	} */
}

//检测手机号码格式
function validateMobileNum(mobileNum){
	var re = /^[1][34578]\d{0,9}$/;
	if(!re.test(mobileNum)){
		return false;
	}else{
		return true;
	}
}


//加载配送订单
function  loadSendOrder(sendData){
	 var url="../../zcAdminRouter?funcID=0042&sendData="+sendData;
	 $("#orderSend").html("");
	 $("#expand-all").hide();
	 $("#order").hide();
	 $(".area-loading").show();
	 $.ajax({  
        type : "get",  
        url :url, 
       /*  async: false, */
        success : function(v){
        	if(v.responseCode=="00000"){
        		var list=v.page.list;
				$("#now").html(v.now);
				if(list.length!=0){
					$.each(list,function(){
						var mtime = new Date(this.endTime);
						var pstatus=this.payStatus;
						var str="已支付";
						var btHtml="<div class='order_send_btn'>"+
										"<a href='javascript:void(0)' sid='"+this.id+"'  onmousedown='endSendOrder(this)' >"+
											"<img src='../../images/order/detail_service.png'>"+
										"</a>"+
									"</div>";
						if(pstatus=='0'){
							str="待支付";
						}
						if(sendData==1){
							btHtml="";
						}
						$("#orderSend").append(
							"<div class='order_send'>"+
								"<div class='order_send_content'>"+
									"<div class='order_send_data'>"+
										"<div class='order_send_item'><p>"+this.gname+" X <font color='red'>"+this.gcount+"</font> ("+str+")</p></div>"+
										"<div class='order_send_user'><p>"+this.userName+"  <a href='tel:"+this.userMp+"'>"+this.userMp+"</a></p></div>"+
									"</div>"+
									btHtml+
								"</div>"+
								"<div class='order_send_addr'><p><font color='red'>"+this.userArea+"</font> "+this.userAddress+"</p></div>"+
							"</div>");
						});
					$(".area-loading").hide();
					$("#order").hide();
					var curpage=v.page.curpage;
	        		var allPage=v.page.allPage;
					if(curpage==allPage){
	        			$("#expand-all").hide();
	        		}else{
	        			$("#expand-all").show();
	        		}
	        		$("#expand-all").attr("curPage",curpage);
	        		$("#expand-all").attr("allPage",allPage);
				}else{
					$(".area-loading").hide();
					$("#otitle").html("您还没有相关的订单！");
					$("#order").show();
				}
        	}else{
        		$(".area-loading").hide();
        		$("#otitle").html("您还没有相关的配送订单！");
        		$("#order").show();
        	}
        },error: function(v){
        	$(".area-loading").hide();
        	$("#otitle").html("订单加载异常！");
        	$("#order").show();
        }
	 });
}
//切换时间
function cutTime(obj){
	var sendData=$(obj).val();
	loadSendOrder(sendData);
}
//刷新页面内容
function refresh(){
	$("#cutTime").val("0");
	$("#search-key").val("");
	loadSendOrder("0");
}
//加载更多配送订单
function  moreSendOrder(obj){
	 var sendData=$("#cutTime").val();
	 var curPage=$(obj).attr("curPage");
	 var params="curPage="+((curPage-0)+1)+"&sendData="+sendData;
	 $(".area-loading").show(); 
	 $(obj).hide();
	 $.ajax({  
	        type : "post",  
	        url : "../../zcAdminRouter?funcID=0042",
	        data:params,
	        success : function(v){
	        	if(v.responseCode=="00000"){
	        		var list=v.page.list;
	        		var sendData=$("#cutTime").val();
					$.each(list,function(){
						var mtime = new Date(this.endTime);
						var pstatus=this.payStatus;
						var str="已支付";
						var btHtml="<div class='order_send_btn'>"+
										"<a href='javascript:void(0)' sid='"+this.id+"'  onmousedown='endSendOrder(this)' >"+
											"<img src='../../images/order/detail_service.png'>"+
										"</a>"+
									"</div>";
						if(pstatus=='0'){
							str="待支付";
						}
						if(sendData==1){
							btHtml="";
						}
						$("#orderSend").append(
							"<div class='order_send'>"+
								"<div class='order_send_content'>"+
									"<div class='order_send_data'>"+
										"<div class='order_send_item'><p>"+this.gname+" X <font color='red'>"+this.gcount+"</font> ("+str+")</p></div>"+
										"<div class='order_send_user'><p>"+this.userName+" <a href='tel:"+this.userMp+"'>"+this.userMp+"</a></p></div>"+
									"</div>"+
									btHtml+
								"</div>"+
								"<div class='order_send_addr'><p><font color='red'>"+this.userArea+"</font> "+this.userAddress+"</p></div>"+
							"</div>");
						});
					var curpage=v.page.curpage;
	        		var allPage=v.page.allPage;
	        		if(curpage==allPage){
	        			$(obj).hide();
	        			$(".area-loading").hide(); 
	        		}else{
	        			$(obj).show();
	        			$(".area-loading").hide(); 
	        		}
	        		$("obj").attr("curPage",curpage);
	        	}else{
	        		layer.open({content: '服务器繁忙！',time: 2});
	        		$(obj).show();
	          		$(".area-loading").hide(); 
	        	}
	        },error: function(v){
	        	layer.open({content: '服务器繁忙！',time: 2}); 
	        	$(obj).show();
	      		$(".area-loading").hide(); 
	        }
		 });
}
//根据手机号查询订单
function searchSendOrderByMp(){
	$("#cutTime").val("0");
	var userMp=$("#search-key").val();
	if(userMp==""){
		 layer.open({content: '请输入手机号！',time: 2});
		 return;
	}
	if(!validateMobileNum(userMp)){
		layer.open({content: '手机号码格式错误！',time: 2}); 
		return;
	}
	$("#orderSend").html("");
	$("#expand-all").hide();
	$("#order").hide();
	$(".area-loading").show();
	var params="userMp="+userMp;
	$.ajax({  
		type : "post",  
        url : "../../zcAdminRouter?funcID=0043",
        data:params,
        success : function(v){
        	if(v.responseCode=="00000"){
        		var list=v.list;
        		$("#now").html(v.now);
				if(list.length!=0){
					$.each(list,function(){
						var mtime = new Date(this.endTime);
						var pstatus=this.payStatus;
						var status=this.status;
						var str="已支付";
						var bg="";
						var btHtml="<div class='order_send_btn'>"+
										"<a href='javascript:void(0)' sid='"+this.id+"'  onmousedown='endSendOrder(this)' >"+
											"<img src='../../images/order/detail_service.png'>"+
										"</a>"+
									"</div>";
						if(pstatus=='0'){
							str="待支付";
						}
						if(status==2){
							bg="background-color: #7ecef4";
							btHtml="";
						}
						$("#orderSend").append(
							"<div class='order_send' style='"+bg+"'>"+
								"<div class='order_send_content'>"+
									"<div class='order_send_data'>"+
										"<div class='order_send_item'><p>"+this.gname+" X <font color='red'>"+this.gcount+"</font> ("+str+")</p></div>"+
										"<div class='order_send_user'><p>"+this.userName+"  <a href='tel:"+this.userMp+"'>"+this.userMp+"</a></p></div>"+
									"</div>"+
									btHtml+
								"</div>"+
								"<div class='order_send_addr'><p><font color='red'>"+this.userArea+"</font> "+this.userAddress+"</p></div>"+
							"</div>");
						});
					$(".area-loading").hide();
	        		$("#expand-all").hide();
	        		$("#order").hide();
				}else{
					$(".area-loading").hide();
					$("#otitle").html("您还没有相关的订单！");
					$("#order").show();
				}
        	}else{
        		$(".area-loading").hide();
        		$("#otitle").html("您还没有相关的配送订单！");
        		$("#order").show();
        	}
        },error: function(v){
        	$(".area-loading").hide();
        	$("#otitle").html("订单加载异常！");
        	$("#order").show();
        }
	 });
}
//结束订单
function endSendOrder(obj){
	var id=$(obj).attr("sid");
	var params="id="+id;
	var index=layer.open({type: 2,shadeClose: false, content: '正在处理中。。。'});
	$.ajax({  
		type : "post",  
        url : "../../zcAdminRouter?funcID=0044",
        data:params,
        success : function(v){
        	layer.close(index);
        	if(v.responseCode=="00000"){
        		if(v.response=="OK"){
        			refresh();
        		}else if(v.response=="not pay"){
        			 layer.open({content: '还没支付不能结束',time: 2});
        		}
        	}else{
        		layer.open({content: '服务器繁忙！',time: 2});
        	}
        },error: function(v){
        	layer.close(index);
        	layer.open({content: '服务器繁忙！',time: 2});
        }
	 });
}